﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySoftLab.DapFlashCore.Probes.DapAccess.Interfaces
{
    public static class HidHelper
    {
        public const int ARM_VID = 0x0d28;
        public const int ATMEL_VID = 0x03eb;
        public const int CYPRESS_VID = 0x04b4;
        public const int KEIL_VID = 0xc251;
        public const int NXP_VID = 0x1fc9;
        public const int VEGA_VID = 0x30cc;

        public const int CMSIS_DAP_HID_USAGE_PAGE = 0xff00;

        public static (int, int) ARM_DAPLINK_ID = (ARM_VID, 0x0204); // Arm DAPLink firmware
        public static (int, int) NXP_LPCLINK2_ID = (NXP_VID, 0x0090); // NXP LPC-LinkII
        public static (int, int) NXP_MCULINK_ID = (NXP_VID, 0x0143); // NXP MCU-Link

        public static (int, int)[] KNOWN_CMSIS_DAP_IDS =
        [
            ARM_DAPLINK_ID,
            (ATMEL_VID, 0x2111), // Microchip EDBG
            (ATMEL_VID, 0x2140), // Microchip JTAGICE3 (firmware version 3 or later)
            (ATMEL_VID, 0x2141), // Microchip Atmel-ICE
            (ATMEL_VID, 0x2144), // Microchip Power Debugger
            (ATMEL_VID, 0x2145), // Microchip mEDBG
            (ATMEL_VID, 0x216c), // Microchip EDBGC
            (ATMEL_VID, 0x2175), // Microchip nEDBG
            (CYPRESS_VID, 0xf138), // Cypress KitProg1, KitProg2 in CMSIS-DAP mode
            (CYPRESS_VID, 0xf148), // Cypress KitProg1, KitProg2 in CMSIS-DAP mode
            (CYPRESS_VID, 0xf151), // Cypress MiniProg4 bulk
            (CYPRESS_VID, 0xf152), // Cypress MiniProg4 HID
            (CYPRESS_VID, 0xf154), // Cypress KitProg3 HID
            (CYPRESS_VID, 0xf155), // Cypress KitProg3 bulk
            (CYPRESS_VID, 0xf166), // Cypress KitProg3 bulk with 2x UART
            (KEIL_VID, 0x2750), // Keil ULINKplus
            (VEGA_VID, 0x9527), // Vega VT-LinkII
            NXP_LPCLINK2_ID,
            NXP_MCULINK_ID,
            (0x1a86, 0x8011),  // WCH-Link
            (0x2a86, 0x8011),  // WCH-Link clone
        ];

        public static (int, int)[] CMSIS_DAP_IDS_TO_FILTER_BY_USAGE_PAGE =
        [
            NXP_LPCLINK2_ID,
            NXP_MCULINK_ID,
        ];

        public static bool IsKnownCmsisDapVidPid(int vid, int pid)
        {
            return KNOWN_CMSIS_DAP_IDS.Contains((vid, pid));
        }

        public static bool FilterDeviceByUsagePage(int vid, int pid, int usagePage)
        {
            return CMSIS_DAP_IDS_TO_FILTER_BY_USAGE_PAGE.Contains((vid, pid));
        }
    }
}
